#ifndef COLLISION_WORLD_H
#define COLLISION_WORLD_H

#include <vector>
using namespace std;

#include "Line.h"
#include "IntersectionDetection.h"
#include "QuadNode.h"
#include "list_reducer.h"
#include <cilk/cilk.h>

#include <cilk/reducer_list.h>
#include "IntersectionInfoList.h"
class CollisionWorld
{
  struct IntersectionInfo {
     Line *l1;
     Line *l2;
     IntersectionType intersectionType;
     IntersectionInfo(Line *l1, Line *l2, IntersectionType
         intersectionType) {
       this->l1 = l1;
       this->l2 = l2;
       this->intersectionType = intersectionType;  
     }
   };
protected:
   // The size of the collision world
   int boxWidth;
   int boxHeight;
   QuadNode* root;

   // Time step used for simulation
   double  timeStep;

   // Container holding all the lines
   vector<Line*> lines;

   // Container holding all intersections
   //list<IntersectionInfo> intersections;
   
   cilk::list_append_reducer_ptr<IntersectionInfo> i_ptr;

   // Record the total number of line wall collision
   unsigned int numLineWallCollisions;

   // Record the total number of line line intersection
   unsigned int numLineLineCollisions;

   IntersectionInfoList_append_reducer inters;
   IntersectionInfoList* iList;
public:
   CollisionWorld();

   // Return the total number of lines in the box
   unsigned int getNumOfLines();

   // Add a line into the box
   void  addLine(Line *line);

   // Get a line from box
   Line *getLine(unsigned int index);

   // Delete all lines in the box
   void  deleteLines();

   //solves collistions in collistion list
   // Update lines' situation in the box
   void  updateLines();

   // Update position of lines
   void  updatePosition();

   // Handle line wall collision
   void lineWallCollision();

   // Detect Line Line intersection
   void detectIntersection(QuadNode *newRoot, vector< vector<Line*>* > spanLineVector);
   
   //detect lintersections among lists of lines
   void intersectVector(vector<Line*> &lineVec1, vector<Line*> &lineVec2); 
   void intersectVector(vector<Line*> &lineVec1); 

   //checks for intersection betweentwo lines
   //adds to intersection list if there is
   void checkIntersection(Line *l1, Line *l2); 

   // Get total number of line wall collisions
   unsigned int getNumLineWallCollisions();

   // Get total number of line line intersection
   unsigned int getNumLineLineCollisions();

   void collisionSolver(Line *l1, Line *l2, IntersectionType intersectionType);


   
};



#endif  /* COLLISION_WORLD_H */
